<?php
/**
 * Created by PhpStorm.
 * User: mojie126
 * Date: 2017/10/17 0017
 * Time: 上午 10:06
 */

namespace app\backend\controller;


use app\index\model\Category;

class TorrentController extends BackendBase
{
	public function category()
	{
		$this->assign("title", "分类管理");
		$this->assign("name", "分类列表");
		$info = db("category")->where("pid", 0)->select();
		$this->assign("c", $info);

		return $this->fetch();
	}

	public function getCategory($type = 0)
	{
		if ($type == 0) {
			$data = input("param.");
			$count = db("category")->count("id");
			$info = db("category")->page($data['page'], $data['limit'])->select();
			foreach ($info as $k => $v) {
				$info[$k]['pid_name'] = Category::getCategoryName($v['pid']);
			}
			$this->jsonReturnLayUI(0, "", $count, $info);
		} elseif ($type == 1) {
			$info = db("category")->where("pid", 0)->select();
			jsonReturn(1, "", $info);
		}
	}

	public function assCategory($type = 0, $id = 0)
	{
		$info = input("param.");
		$db = "category";
		$data = [
			"name" => $info['name'],
			"pid" => $info['pid'],
			"sort" => $info['sort'],
			"display" => $info['display']
		];
		if ($type == 0) {
			try {
				db($db)->insert($data);
				jsonReturn(1, "新增成功");
			} catch (\Exception $exception) {
				jsonReturn(0, "新增失败");
			}
		} elseif ($type == 1) {
			if (empty($id)) {
				jsonReturn(0, "参数错误");
			}
			try {
				db($db)->where("id", $id)->delete();
				jsonReturn(1, "删除成功");
			} catch (\Exception $exception) {
				jsonReturn(0, "删除失败");
			}
		}
	}

	public function editCategory($id, $k, $v)
	{
		try {
			db("category")->where("id", $id)->update([$k => $v]);
			jsonReturn(1, "修改成功", ["name" => Category::getCategoryName($v)]);
		} catch (\Exception $exception) {
			jsonReturn(0, "修改失败");
		}
	}
}